package com.zhengsanming.web.old;

import com.alibaba.fastjson.JSON;
import com.zhengsanming.pojo.User;
import com.zhengsanming.pojo.respdate;
import com.zhengsanming.service.UserServive;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private UserServive servive = new UserServive();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Credentials", "true");

//        HttpSession session = req.getSession();
//        String ss = (String) session.getAttribute("checkCode");
//        System.out.println(ss);
        req.setCharacterEncoding("utf-8");
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        User user = JSON.parseObject(s, User.class);
        Boolean register = servive.register(user);
//        String code = user.getCode();
//        System.out.println(code);
//        System.out.println(ss);

        // if (true) {
//            System.out.println("sucess");
//            resp.setContentType("text/json;charset=utf-8");
//            respdate respdate = new respdate();
//            respdate.setMsg("验证码错误");
//            respdate.setStatus("401");
//            respdate.setData("fasle");
//            String o = JSON.toJSONString(respdate);
//            resp.getWriter().write(o);
        // }
        if (register) {
            resp.setContentType("text/json;charset=utf-8");
            respdate respdate = new respdate();
            respdate.setMsg("注册成功");
            respdate.setStatus("200");
            respdate.setData("true");
            String o = JSON.toJSONString(respdate);
            resp.getWriter().write(o);
        } else {
            resp.setContentType("text/json;charset=utf-8");
            respdate respdate = new respdate();
            respdate.setMsg("用户已存在");
            respdate.setStatus("401");
            respdate.setData("true");
            String o = JSON.toJSONString(respdate);
            resp.getWriter().write(o);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
